home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / quodlibet / plugins / playlist.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  8KB  |  225 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from gi.repository import Gtk
  5. from quodlibet.qltk import get_top_parent
  6. from quodlibet.qltk.msg import WarningMessage
  7. from quodlibet.qltk.x import SeparatorMenuItem, Button
  8. from quodlibet.util import print_exc
  9. from quodlibet.util.dprint import print_d, print_e
  10. from quodlibet import qltk
  11. from quodlibet.plugins import PluginHandler, PluginManager
  12. from quodlibet.plugins.gui import MenuItemPlugin
  13. from quodlibet.util import connect_obj
  14.  
  15. class ConfirmMultiPlaylistInvoke(WarningMessage):
  16.     '''Dialog to confirm invoking a plugin with X playlists
  17.     in case X is high
  18.     '''
  19.     RESPONSE_INVOKE = 1
  20.     
  21.     def __init__(self, parent, plugin_name, count):
  22.         title = ngettext('Run the plugin "%s" on %d playlist?', 'Run the plugin "%s" on %d playlists?', count) % (plugin_name, count)
  23.         super(ConfirmMultiPlaylistInvoke, self).__init__(get_top_parent(parent), title, '', buttons = Gtk.ButtonsType.NONE)
  24.         self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
  25.         delete_button = Button(_('_Run Plugin'), Gtk.STOCK_EXECUTE)
  26.         delete_button.show()
  27.         self.add_action_widget(delete_button, self.RESPONSE_INVOKE)
  28.         self.set_default_response(Gtk.ResponseType.CANCEL)
  29.  
  30.     
  31.     def confirm(cls, parent, plugin_name, count):
  32.         '''Returns if the action was confirmed'''
  33.         resp = cls(parent, plugin_name, count).run()
  34.         return resp == cls.RESPONSE_INVOKE
  35.  
  36.     confirm = classmethod(confirm)
  37.  
  38.  
  39. class PlaylistPlugin(MenuItemPlugin):
  40.     """
  41.     Playlist plugins are much like songsmenu plugins,
  42.     and provide one or more of the following instance methods:
  43.  
  44.         self.plugin_single_playlist(playlist)
  45.         self.plugin_playlist(song)
  46.         self.plugin_playlists(songs)
  47.  
  48.     All matching provided callables on a single object are called in the
  49.     above order if they match until one returns a true value.
  50.  
  51.     The single_ variant is only called if a single song/album is selected.
  52.  
  53.     The singular tense is called once for each selected playlist, but the
  54.     plural tense is called with a list of playlists
  55.  
  56.     Returning `True` from these signifies a change was made and the UI /
  57.     library should update; otherwise this isn't guaranteed.
  58.  
  59.     Currently (01/2014) only the singular forms are actually supported in
  60.     the UI, but this won't always be the case.
  61.  
  62.     To make your plugin insensitive if unsupported playlists are selected,
  63.     a method that takes a list of songs and returns True or False to set
  64.     the sensitivity of the menu entry:
  65.         self.plugin_handles(playlists)
  66.  
  67.     All of this is managed by the constructor, so
  68.     make sure it gets called if you override it (you shouldn't have to).
  69.  
  70.     TODO: A way to inherit from both PlaylistPlugin and SongsMenuPlugin
  71.     """
  72.     plugin_single_playlist = None
  73.     plugin_playlist = None
  74.     plugin_playlists = None
  75.     
  76.     def __init__(self, playlists, library, window):
  77.         super(PlaylistPlugin, self).__init__(window)
  78.         self._library = library
  79.         self.set_sensitive(bool(self.plugin_handles(playlists)))
  80.  
  81.     
  82.     def plugin_handles(self, playlists):
  83.         return True
  84.  
  85.  
  86.  
  87. class PlaylistPluginHandler(PluginHandler):
  88.     '''Handles PlaylistPlugins'''
  89.     
  90.     def init_plugins(self):
  91.         PluginManager.instance.register_handler(self)
  92.  
  93.     
  94.     def __init__(self, confirmer = None):
  95.         '''custom confirmer mainly for testing'''
  96.         self._PlaylistPluginHandler__plugins = []
  97.         if confirmer is None:
  98.             self._confirm_multiple = ConfirmMultiPlaylistInvoke.confirm
  99.         else:
  100.             self._confirm_multiple = confirmer
  101.  
  102.     
  103.     def populate_menu(self, menu, library, browser, playlists):
  104.         '''Appends items onto `menu` for each enabled playlist plugin,
  105.         separated as necessary. '''
  106.         top_parent = qltk.get_top_parent(browser)
  107.         attrs = [
  108.             'plugin_playlist',
  109.             'plugin_playlists']
  110.         if len(playlists) == 1:
  111.             attrs.append('plugin_single_playlist')
  112.         items = []
  113.         kinds = self._PlaylistPluginHandler__plugins
  114.         kinds.sort(key = (lambda plugin: plugin.PLUGIN_ID))
  115.         print_d('Found %d Playlist plugin(s): %s' % (len(kinds), kinds))
  116.         for Kind in kinds:
  117.             usable = any([ callable(getattr(Kind, s)) for s in attrs ])
  118.             if usable:
  119.                 
  120.                 try:
  121.                     items.append(Kind(playlists, library, top_parent))
  122.                 print_e("Couldn't initialise playlist plugin %s: " % Kind)
  123.                 print_exc()
  124.  
  125.                 continue
  126.         items = filter((lambda i: i.initialized), items)
  127.         if items:
  128.             menu.append(SeparatorMenuItem())
  129.             for item in items:
  130.                 
  131.                 try:
  132.                     menu.append(item)
  133.                     args = (library, browser, playlists)
  134.                     if item.get_submenu():
  135.                         for subitem in item.get_submenu().get_children():
  136.                             connect_obj(subitem, 'activate', self._PlaylistPluginHandler__handle, item, *args)
  137.                         
  138.                     else:
  139.                         item.connect('activate', self._PlaylistPluginHandler__handle, *args)
  140.                 continue
  141.                 print_exc()
  142.                 item.destroy()
  143.                 continue
  144.  
  145.             
  146.  
  147.     
  148.     def handle(self, plugin_id, library, browser, playlists):
  149.         '''Start a plugin directly without a menu'''
  150.         for plugin in self._PlaylistPluginHandler__plugins:
  151.             if plugin.PLUGIN_ID == plugin_id:
  152.                 
  153.                 try:
  154.                     plugin = plugin(playlists, library, browser)
  155.                 except Exception:
  156.                     print_exc()
  157.  
  158.                 self._PlaylistPluginHandler__handle(plugin, library, browser, playlists)
  159.                 return None
  160.         
  161.  
  162.     
  163.     def __handle(self, plugin, library, browser, playlists):
  164.         if len(playlists) == 0:
  165.             return None
  166.         if None(playlists) == 1 and callable(plugin.plugin_single_playlist):
  167.             pl = playlists[0]
  168.             
  169.             try:
  170.                 ret = plugin.plugin_single_playlist(pl)
  171.             except Exception:
  172.                 print_exc()
  173.  
  174.             if ret:
  175.                 print_d('Updating %s' % pl)
  176.                 browser.changed(pl)
  177.                 browser.activate()
  178.                 return None
  179.         if callable(plugin.plugin_playlist):
  180.             total = len(playlists)
  181.             if not total > plugin.MAX_INVOCATIONS and self._confirm_multiple(browser, plugin.PLUGIN_NAME, total):
  182.                 return None
  183.             
  184.             try:
  185.                 ret = map(plugin.plugin_playlist, playlists)
  186.                 if ret:
  187.                     for update, pl in zip(ret, playlists):
  188.                         if update:
  189.                             print_d('Updating %s' % pl)
  190.                             browser.changed(pl)
  191.                             continue
  192.                     browser.activate()
  193.             except Exception:
  194.                 print_exc()
  195.  
  196.             if max(ret):
  197.                 return None
  198.         if callable(plugin.plugin_playlists):
  199.             
  200.             try:
  201.                 if plugin.plugin_playlists(playlists):
  202.                     browser.activate()
  203.             except Exception:
  204.                 print_exc()
  205.                 for pl in playlists:
  206.                     browser.changed(pl)
  207.                 
  208.             
  209.  
  210.  
  211.     
  212.     def plugin_handle(self, plugin):
  213.         return issubclass(plugin.cls, PlaylistPlugin)
  214.  
  215.     
  216.     def plugin_enable(self, plugin):
  217.         self._PlaylistPluginHandler__plugins.append(plugin.cls)
  218.  
  219.     
  220.     def plugin_disable(self, plugin):
  221.         self._PlaylistPluginHandler__plugins.remove(plugin.cls)
  222.  
  223.  
  224. PLAYLIST_HANDLER = PlaylistPluginHandler()
  225.